package com.zkx.mvp.adapter.chat;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zkx.mvp.R;
import com.zkx.mvp.interfaces.ItemClickListener;
import com.zkx.mvp.model.chat.MsgItem;
import com.zkx.mvp.picasso.PicassoCircleTransform;

import java.util.List;

/**
 * Author：zhangkx on 2017/6/29 14:15
 * Email：zkx9025@163.com
 * 消息列表适配器
 */

public class MsgListAdapter extends RecyclerView.Adapter<MsgListAdapter.MsgListViewHolder> {
    private Context context;
    private List<MsgItem> mList;
    /**
     * Item是否被选中监听
     */
    private OnCheckedChangeListener mOnCheckedChangeListener;
    public void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }
    /**
     * item 点击监听
     */
    private ItemClickListener<MsgItem> mItemClickListener ;
    public  void  setItemClick(ItemClickListener<MsgItem> listener ){
        this.mItemClickListener = listener;
    }
    /**
     * Item拖拽滑动帮助
     */
    private ItemTouchHelper itemTouchHelper;
    public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
        this.itemTouchHelper = itemTouchHelper;
    }

    public MsgListAdapter(Context context,List<MsgItem> mList) {
        super();
        this.context = context;
        this.mList = mList;
    }

    /**
     * 刷新数据
     * @param userInfos
     */
    public void notifyDataSetChanged(List<MsgItem> userInfos) {
        this.mList = userInfos;
        super.notifyDataSetChanged();
    }

    @Override
    public MsgListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
        return new MsgListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MsgListViewHolder holder, int position) {
        holder.setData();
    }

    @Override
    public int getItemCount() {
        return mList ==null?0:mList.size();
    }
    public MsgItem getData(int position) {
        return mList.get(position);
    }

    /**
     * 获取列表
     * @return
     */
    public List<MsgItem> getList(){
        return mList;
    }

    /**
     * 初始化viewHolder
     */
    class MsgListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener{
        /**
         * 名字和性别
         */
        private TextView mTvName, mTvSex;
        /**
         * 触摸就可以拖拽
         */
        private ImageView mIvTouch;
        /**
         * 触摸就可以拖拽
         */
        private ImageView ivUser;
        /**
         * 是否选中
         */
        private CheckBox mCbCheck;
        public MsgListViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mTvSex = (TextView) itemView.findViewById(R.id.tv_sex);
            mIvTouch = (ImageView) itemView.findViewById(R.id.iv_touch);
            ivUser = (ImageView) itemView.findViewById(R.id.ivUser);
            mCbCheck = (CheckBox) itemView.findViewById(R.id.cb_item_check);
            mCbCheck.setOnClickListener(this);
            mIvTouch.setOnTouchListener(this);
        }

        /**
         * 给这个Item设置数据
         */
        public void setData() {
            MsgItem userInfo = getData(getAdapterPosition());
            mTvName.setText(userInfo.getName());
            mTvSex.setText(userInfo.getDescription());
            mCbCheck.setChecked(userInfo.isCheck());
            Picasso.with(ivUser.getContext()).
                    load(userInfo.getIdImage()).
                    centerCrop().
                    resize(60,60).
                    transform(new PicassoCircleTransform()).
                    into(ivUser);
        }

        @Override
        public void onClick(View view) {
            if (view == itemView && itemTouchHelper != null) {
                mItemClickListener.onItemClick(view, getData(getAdapterPosition()));
            } else if (view == mCbCheck && mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.onItemCheckedChange(mCbCheck, getAdapterPosition(), mCbCheck.isChecked());
            }
        }

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (view == mIvTouch)
                itemTouchHelper.startDrag(this);
            return false;
        }
    }

    public interface OnCheckedChangeListener {
        void onItemCheckedChange(CompoundButton view, int position, boolean checked);
    }
}
